<?php
namespace app\manager\controller;
use think\Controller;
use think\Request;
use \app\common\traits\controller\TraitController;
/**
 * 管理员控制器
 */
class CampManager extends BaseController
{
    /**
     * 提交登录信息
     * @return [type] [description]
     */
    public function postLogin()
    {
        //$id = session('manager.id');halt($id);
        $code = input('post.code');
        $captcha = new \think\captcha\Captcha();
        if (!$captcha->check($code)) {
            return $this->error('验证码错误');
        }
        $manager_phone = input('post.phone');//halt($manager_phone);
        $manager_password = input('post.password');
        $manager_remember_me = input('post.remember_me');
        $manager = model('CampManagers')->accountLogin($manager_phone,$manager_password, false);//halt(session('manager'));
        if ($manager) {
            model('CampManagers')->where('phone',$manager_phone)->setField('login_session_id',session('manager.login_session_id'));
            return $this->success('登录成功','/campmanagerlist');
        }
        return $this->error(lang('login failed').model('CampManagers')->getError());
    }
    /**
     * 登录表单
     * @return [type] [description]
     */
    public function getLogin()
    {
        if (model('CampManagers')->checkLogin()) {
            $this->redirect('/');
        }else{
            return view('index/login');
        }
    }
    /**
     * 登出
     * @return [type] [description]
     */
    public function getlogout()
    {
        if (model('CampManagers')->logOut()) {
            return $this->success(lang('logout success'), '/campmanager/login');
        }    
        return $this->error(lang('logout failed'));    
    }
    /**忘记密码
     * [getModifyPassword description]
     * @return [type] [description]
     */
    public function getForgetPassword()
    {
        return view('index/forgetpwd');
    }

    /**
     * [getPersonModifyPwd description]
     * @return [type] [description]
     */
    public function getPersonModifyPwd()
    {
        return view('index/personmodifypwd');
    }

    /**
     * [postPersonModifyPwd description]
     * @return [type] [description]
     */
    public function postPersonModifyPwd()
    {
        # code...
    }
    //发送密码到手机
    public function postSendPwd()
    {
        $phone = input('post.phone');
        $info = model('CampManagers')->where('phone',$phone)->find();
        if ($info == '') {
            return $this->error('您的手机号不存在');
        }
        $pwd = $this->getInvitation();
        
        $foo = \services\SmsFactory::init('WmSms')->sendCaptcha($phone,$pwd,'10');

        return model('CampManagers')->setPwdSession($phone,$pwd);
    }

    public function postNewPwd()
    {
        $phone = input('post.phone');
        $nwepwd = input('post.newpwd');
        $sendpwd = session('admin.pwd');
        if ($nwepwd == $sendpwd) {
            $nwepwd = md5($nwepwd);
            $count = model('CampManagers')->revisePwd($phone,$nwepwd);
                if ($count) {
                    model('CampManagers')->clearPwdSession();
                    return $this->success('新密码设置成功','/campmanagerlist');
            }
        }
        return $this->error('新密码设置失败');
    }
}
